package com.bjb.vr.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.exception.CommonException;
import com.bjb.vr.common.utils.DateTimeUtil;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.UmsAccountVo;
import com.github.yitter.idgen.YitIdHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @version 1.0.0
 * @author: HuJingBo
 * @time: 2022/7/15 15:26
 */
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
        // 如果为空，提前报错提醒
        if (Objects.isNull(umsAccountVo)) {
            throw new CommonException("当前请求未设置租户信息,请设置租户信息通过ThreadLocal【ThreadLocalUtil.set(ThreadLocalConstant.USER_ACCOUNT_INFO, umsAccountVo);】");
        }
        // 设置租户ID,tempTenantId优先级比较高
        if (StringUtils.isNotBlank(umsAccountVo.getTenantId())) {
            this.setFieldValByName("tenantId", umsAccountVo.getTenantId(), metaObject);
        }
        if (StringUtils.isNotBlank(umsAccountVo.getTempTenantId())) {
            this.setFieldValByName("tenantId", umsAccountVo.getTempTenantId(), metaObject);
        }
        this.setFieldValByName("creator", umsAccountVo.getUsername(), metaObject);
        this.setFieldValByName("accountId", umsAccountVo.getUserId(), metaObject);
        this.setFieldValByName("gmtCreate", DateTimeUtil.getLocalDateTime(new Date()), metaObject);
        // 判断是否包含数据ID
        String dataIdProp = metaObject.findProperty("dataId", false);
        if (StringUtils.isNotBlank(dataIdProp)) {
            // 判断数据ID的值是否为空
            Long dataId = (Long) metaObject.getValue("dataId");
            if (Objects.isNull(dataId)) {
                this.setFieldValByName("dataId", YitIdHelper.nextId(), metaObject);
            }
        }
        // 设置用户ID
        String userIdProp = metaObject.findProperty("userId", false);
        if (StringUtils.isNotBlank(userIdProp)) {
            // 判断数据ID的值是否为空
            Long userId = (Long) metaObject.getValue("userId");
            if (Objects.isNull(userId)) {
                this.setFieldValByName("userId", YitIdHelper.nextId(), metaObject);
            }
        }
        // 设置角色ID
        String roleIdProp = metaObject.findProperty("roleId", false);
        if (StringUtils.isNotBlank(roleIdProp)) {
            // 判断数据ID的值是否为空
            Long roleId = (Long) metaObject.getValue("roleId");
            if (Objects.isNull(roleId)) {
                this.setFieldValByName("roleId", YitIdHelper.nextId(), metaObject);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("gmtModified", DateTimeUtil.getLocalDateTime(new Date()), metaObject);
        this.setFieldValByName("loginTime", DateTimeUtil.getLocalDateTime(new Date()), metaObject);
        this.setFieldValByName("gmtUpdate", DateTimeUtil.getLocalDateTime(new Date()), metaObject);
        this.setFieldValByName("version", this.getFieldValByName("version", metaObject), metaObject);
    }
}
